package com.wing.warehouse.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.chanjet.entity.warehouse.response.WarehouseQueryResponse;
import com.wing.chanjet.feign.ChanjetClient;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.HttpHelper;
import com.wing.common.utils.JsonResult;
import com.wing.platform.feign.PlatformClient;
import com.wing.warehouse.dao.WarehouseDao;
import com.wing.warehouse.model.entity.Warehouse;
import com.wing.warehouse.model.entity.WarehouseType;
import com.wing.warehouse.model.request.WarehouseForm;
import com.wing.warehouse.model.response.WarehouseVO;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 仓库-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class WarehouseServiceImpl extends ServiceImpl<WarehouseDao, Warehouse> implements WarehouseService {

    @Resource
    ChanjetClient chanjetClient;

    @Resource
    WarehouseTypeService warehouseTypeService;

    @Value("${platform.gatewayUrl}")
    private String gatewayUrl;

    @Override
    public int add(WarehouseForm warehouseForm) {
        //校验
        Warehouse warehouse = new Warehouse();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(Warehouse.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(warehouseForm, warehouse, copyOptions);
        return baseMapper.insert(warehouse);
    }

    @Override
    public List<WarehouseVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<WarehouseVO> pageList4app(Page page, Map<String, Object> params) {
        List<WarehouseVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public WarehouseVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }


    @Override
    public void chanjetWarehouseSync() {
        //仓库列表
        List<Warehouse> list = this.list();

        List<Warehouse> warehouseList = new ArrayList<>();

        JsonResult<List<WarehouseQueryResponse>> jsonResult = chanjetClient.warehouseQuery();
        if (jsonResult.getCode() == 200) {
            List<WarehouseQueryResponse> warehouseQueryList = jsonResult.getData();
            for (WarehouseQueryResponse warehouseQueryResponse : warehouseQueryList) {
                Warehouse warehouse = new Warehouse();
                warehouse.setCode(warehouseQueryResponse.getCode());
                warehouse.setName(warehouseQueryResponse.getName());
                warehouse.setWarehouseFeatureType("1"); // 默认：自建
                warehouse.setWorkbinStatus("0");
                warehouse.setHasPosition(warehouseQueryResponse.getHasPosition());
                //仓库类型
                QueryWrapper<WarehouseType> warehouseTypeQueryWrapper = new QueryWrapper<>();
                warehouseTypeQueryWrapper.eq("code", warehouseQueryResponse.getWarehouseType().getCode());
                WarehouseType warehouseType = warehouseTypeService.getOne(warehouseTypeQueryWrapper);
                if (warehouseType == null) {
                    warehouseType = new WarehouseType();
                    warehouseType.setCode(warehouseQueryResponse.getWarehouseType().getCode());
                    warehouseType.setName(warehouseQueryResponse.getWarehouseType().getName());
                    warehouseType.setCreateTime(LocalDateTime.now());
                    warehouseType.setCreator(JwtUtils.getLoginUserId());
                    warehouseTypeService.save(warehouseType);
                }
                warehouse.setWarehouseTypeId(warehouseType.getId());

                //判断仓库编码是否存在
                Warehouse warehouseByCode = list.stream().filter(t->t.getCode().equals(warehouse.getCode())).findFirst().orElse(null);
                if(warehouseByCode != null){
                    warehouse.setId(warehouseByCode.getId());
                }
                warehouseList.add(warehouse);
            }
            //批量新增
            this.saveOrUpdateBatch(warehouseList);
        } else {
            throw new BusinessException("畅捷通服务-仓库查询接口调用失败");
        }
    }

}
